package com.foreveross.proxyip.local;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.foreveross.crawl.common.util.RegHtmlUtil;
import com.foreveross.crawl.common.util.StringUtils;

public class HttpDownProxyIp {
	public Logger logger = LoggerFactory.getLogger(HttpDownProxyIp.class);
	private static HttpDownProxyIp _instance = null;
	
	private static final String IP_REGEX =  "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,5}$";
	
	public synchronized static HttpDownProxyIp getInstance(){
		if(_instance == null){
			_instance = new HttpDownProxyIp();
		}
		return _instance;
	}
	
	public List<String> downloadIps(int num) throws Exception{
		List<String> proxyips = new ArrayList<String>();
		int c=4;
		while(c-->0){
			int rd = RandomUtils.nextInt(7);
			switch(rd){
			case 0:
				proxyips.addAll(downXinxinproxyIps(num));
				break;
			case 1:
				proxyips.addAll(downDaili73(num));
				break;
			case 2:
				proxyips.addAll(daili232(num));
				break;
			case 3:
				//proxyips.addAll(dailiYun(num));
				proxyips.addAll(this.dailiTk(num));
				break;
			case 4:
				proxyips.addAll(daili73(num));
				break;
			case 5:
				proxyips.addAll(this.downDaili666Ips(num));
				break;
			case 6:
				proxyips.addAll(dailiIphai(num));
				break;
			}
			if(!proxyips.isEmpty()){
				break;
			}
		}
		return proxyips;
	}
	
	//http://121.199.38.28/api/v1/ip.txt?tid=262304270418529&num=1&operator=&area=&filter=off
	private List<String> downXinxinproxyIps(int num) throws Exception{
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpclient = new DefaultHttpClient();
		String url = "http://www.xinxinproxy.com/httpip/text?count="+num+"&orderId=196622308412141&isNew=1&ports=80,8080,8089,6666,6675";
		HttpGet httpGet = new HttpGet(url);

		 HttpResponse response =httpclient.execute(httpGet);   
	     HttpEntity entity = response.getEntity();  
	     String sHtml = EntityUtils.toString(entity);
	     if(StringUtils.isNotEmpty(sHtml)){
//	    	 if(logger.isInfoEnabled()) logger.info("xinxinproxy gethtml:"+sHtml.substring(0, 100));
	    	 for(String s : sHtml.split("\n")){
	    		 if(StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
	    			 proxyips.add(s.trim());
	    		 }
	    	 }
	     }
		return proxyips;
	}
	
	@SuppressWarnings("unused")
	private List<String> downDaili666Ips(int num) throws Exception{
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpclient = new DefaultHttpClient();
		String url = "http://121.199.38.28/api/v1/ip.txt?tid=736984901268529&num="+num+"&operator=&area=&filter=off";
		HttpGet httpGet = new HttpGet(url);

		 HttpResponse response =httpclient.execute(httpGet);   
	     HttpEntity entity = response.getEntity();  
	     String sHtml = EntityUtils.toString(entity);
	     if(StringUtils.isNotEmpty(sHtml)){
	    	 if(logger.isInfoEnabled()) logger.info("daili666 gethtml:"+sHtml.substring(0, 100));
	    	 for(String s : sHtml.split("\n")){
	    		 if(StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
	    			 proxyips.add(s.trim());
	    		 }
	    		
	    	 }
	     }
		return proxyips;
	}
	//http://222.186.50.73:8888/api.asp?spm=a1z09.6.0.97.gIO63g&ddbh=LP00015573556641&old=1&noinfo=true&sl=200&disp=1
	private List<String> downDaili73(int num) throws Exception{
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpclient = new DefaultHttpClient();
		String url = "http://222.186.50.73:8888/api.asp?spm=a1z09.6.0.97.gIO63g&ddbh=LP00015573556641&old=1&noinfo=true&sl="+num;
		HttpGet httpGet = new HttpGet(url);

		 HttpResponse response =httpclient.execute(httpGet);   
	     HttpEntity entity = response.getEntity();  
	     String sHtml = EntityUtils.toString(entity);
	     if(StringUtils.isNotEmpty(sHtml)){
//	    	 if(logger.isInfoEnabled()) logger.info("Daili2221865073 gethtml:"+sHtml.substring(0, 100));
	    	 for(String s : sHtml.split("\n")){
	    		 if(StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
	    			 proxyips.add(s.trim());
	    		 }
	    		
	    	 }
	     }
		return proxyips;
	}
	
	
	
	private List<String> daili232(int num){
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://60.173.11.232:2222/api.asp?ddbh=571184908488529&dk=" +
				"8088%2C18186%2C8080%2C3128%2C80%2C1998%2C9999%2C9000%2C8888%2C63000%2C3129%2C1337%2C81%2C8000%2C808%2C1175%2C8081%2C8123%2C8118%2C443%2C8424"+
				"&old=on"+"&sl="+num;
		try {
			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
				for(String s : html.split("\n")){
					String ip=RegHtmlUtil.regStr(s, ".*>(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}:\\d{1,5})");//端口有5位的
					if(ip!=null){
						proxyips.add(ip);
					}
				}
			}
		} catch (Exception e) {
			logger.info(String.format("[%s] 提取IP出错[%s]","代理2222", ExceptionUtils.getFullStackTrace(e)));
		}
		return proxyips;
	}
	
	private List<String> dailiYun(int num){
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://www.yun-ip.com/api.php?key=570787797918529&getnum="+num;
		try {
			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
				for(String s : html.split("\n")){
					if(org.apache.commons.lang3.StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
						proxyips.add(s.replaceAll("\\s", ""));
					}
				}
			}
		} catch (Exception e) {
			logger.info(String.format("[%s] 提取IP出错[%s]","代理云", ExceptionUtils.getFullStackTrace(e)));
		}
		return proxyips;
	}
	
	
	private List<String> daili73(int num){
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://222.186.50.73:8888/api.asp?" +
				"button=%C3%88%C2%B7%20%20%C2%B6%C2%A8&ddbh=LP00015573556641&" +
				"dk18186=18186&dk1998=1998&dk3128=3128&dk3129=3129&dk63000=63000&" +
				"dk80=80&dk8080=8080&dk8088=8088&dk8888=8888&dk9000=9000&" +
				"dk9999=9999&dkstr=8088%2C18186%2C8080%2C3128%2C80%2C1998%2C9999%2C8888%2C63000%2C3129%2C9000&downtxt=true&dq=&kt=&" +
				"sl="+num+"&yy=";
		try {
			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
				for(String s : html.split("\n")){
					if(org.apache.commons.lang3.StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
						proxyips.add(s.replaceAll("\\s", ""));
					}
				}
			}
		} catch (Exception e) {
			logger.info(String.format("[%s] 提取IP出错[%s]","代理73", ExceptionUtils.getFullStackTrace(e)));
		}
		return proxyips;
	}
	
//	private List<String> daili112(int num){
//		List<String> proxyips = new ArrayList<String>();
//		DefaultHttpClient httpClient = new DefaultHttpClient();
//		String url="http://112.124.9.190:80/http/sys/tbHttpip_getAPI?" +
//				"orderId=659505456538529&isDetailFormat=&includePorts=" +
//				"&excludePorts=&isps=&includeProvinces=&excludeProvinces=&count="+num+"&startWith=";
//		try {
//			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
//			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
//				for(String s : html.split("<br/>")){
//					if(org.apache.commons.lang3.StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
//						proxyips.add(s.replaceAll("\\s", ""));
//					}
//				}
//			}
//		} catch (Exception e) {
//			logger.info(String.format("[%s] 提取IP出错[%s]","代理112", ExceptionUtils.getFullStackTrace(e)));
//		}
//		return proxyips;
//	}
	
	private List<String> dailiIphai(int num){
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://www.iphai.com/getapi.ashx?ddh=659172589698529&num="+num+"&yys=1&am=0&mt=6&fm=text";
		try {
			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
				for(String s : html.split("\n")){
					if(org.apache.commons.lang3.StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
						proxyips.add(s.replaceAll("\\s", ""));
					}
				}
			}
		} catch (Exception e) {
			logger.info(String.format("[%s] 提取IP出错[%s]","代理Iphai", ExceptionUtils.getFullStackTrace(e)));
		}
		return proxyips;
	}
	private List<String> dailiTk(int num){
		List<String> proxyips = new ArrayList<String>();
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url="http://www.tkdaili.com/api/getipen.aspx?vkey=316801342C19FF3C22D2F1480303E65188CAA7A195BA97AB120DD1A2D261DB520817BF6E372EB25F44E206444E885E3B8A371E781C18C256";
		try {
			String html=EntityUtils.toString(httpClient.execute(new HttpGet(url)).getEntity());
			if(org.apache.commons.lang3.StringUtils.isNotBlank(html)){
				for(String s : html.split("\n")){
					if(org.apache.commons.lang3.StringUtils.isNotEmpty(s) && RegHtmlUtil.regMathcher(s, IP_REGEX)){
						proxyips.add(s.replaceAll("\\s", ""));
					}
				}
			}
		} catch (Exception e) {
			logger.info(String.format("[%s] 提取IP出错[%s]","代理Iphai", ExceptionUtils.getFullStackTrace(e)));
		}
		return proxyips;
	}
//	public static void main(String[] arg) throws Exception{
//		List<String> proxyIps = HttpDownProxyIp.getInstance().dailiTk(200);
//
//		for(String ip : proxyIps){
//			System.out.println(ip);
//		}
//	}
}
